/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package framework.db;

import framework.config.Database;
import java.sql.PreparedStatement;

/**
 *
 * @author Darlan
 */
public class SequenciaManager
{
    private static SequenciaManager instance;

    /**
     * 
     * @return 
     */
    public static SequenciaManager getInstance()
    {
        if ( instance == null )
        {
            instance = new SequenciaManager();
        }

        return instance;
    }
    
    /**
     * 
     * @return
     * @throws Exception 
     */
    private Database getDatabase() throws Exception
    {
        return Database.getInstance();
    }
    
    /**
     * 
     */
    public int getSequencia( String tabela ) throws Exception
    {
        Database db = getDatabase();
        
//        try
//        {
            String sql = "select sequencia from sequencia_tabelas " + 
                    " where tabela = ? ";

            PreparedStatement ps = db.prepareStatement( sql );
            
            ps.setString( 1, tabela );
            int id = db.fetchInt( ps );
            
            sql = "update sequencia_tabelas set sequencia = ? " + 
                    " where tabela = ? ";
            
            PreparedStatement ps2 = db.prepareStatement( sql );
            
            ps2.setInt( 1, id + 1 );
            ps2.setString( 2, tabela );
            
            ps2.execute();
            
            return id;
//        }
//        
//        finally
//        {
//            db.close();
//        }
    }
}
